﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CNTVNow.Web.Models;
using CNTVNow.Web.Helpers.Common;

namespace CNTVNow.Web.Controllers
{
    [HandleError]
    public class HomeController : BaseController
    {
        HomeModel homeModel = new HomeModel();
        public ActionResult Index()
        {
            Session["category"] = FixCommon.NewsHomeId;
            try
            {
                ViewData["NewsBanner"] = homeModel.GetNewsTopBanner();
                ViewData["NewsUnderBanner"] = homeModel.GetNewsUnderTopBanner();
                ViewData["NewsLast"] = homeModel.GetNewsLatest();
                ViewData["NewsLeft"] = homeModel.GetNewsLeft();
                ViewData["NewsMiddle"] = homeModel.GetNewsMiddle();

                ViewData["AdsTop"] = SingletonContext.NewsModelInstance.GetAdsItem(FixCommon.AdsHome, FixCommon.AdsTop);
                ViewData["AdsLeft"] = SingletonContext.NewsModelInstance.GetAdsItem(FixCommon.AdsHome, FixCommon.AdsLeft);
                ViewData["AdsRight"] = SingletonContext.NewsModelInstance.GetAdsItem(FixCommon.AdsHome, FixCommon.AdsRight);
            }
            catch
            {
                //Future write log
            }
            return View();
        }
        [HttpPost]
        public ActionResult Search(FormCollection formCollection)
        {
            if (Session["category"] != null)
            {
                List<New> lstNews;
                NewsModel newModel = new NewsModel();
                int categoryId= int.Parse(Session["category"].ToString());
                if (categoryId == 0)
                {
                    lstNews = newModel.GetNewsBySearchFull(formCollection["txtHeaderSearch"]);
                }
                else
                {
                    lstNews = newModel.GetNewsBySearchByCategory( formCollection["txtHeaderSearch"],categoryId);
                }
                ViewData["datasearch"] = lstNews;
            }
            return View();
        }
    }
}
